package framework.controller;

import framework.util.fileUpload.bo.SysFileModule;
import framework.bo.SysWorker;
import framework.common.ResponseVo;
import framework.util.fileUpload.service.SysFileModuleService;
import framework.util.fileUpload.service.SysFileService;
import framework.util._BeanUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import frameworkExt.util.controller.BaseControllerExt;
import framework.service.SysWorkerService;;import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/framework/SysWorker.do")
public class SysWorkerController extends BaseControllerExt {

    @Resource
    private SysWorkerService service;
    @Resource
    private SysFileModuleService sysFileModuleService;
    @Resource
    private SysFileService sysFileService;

    @ResponseBody
    @RequestMapping(params = "method=get")
    public ResponseVo get(HttpServletRequest request){
        String lid = request.getParameter("lid");
        if(StringUtils.isAnyBlank(lid)){
            sendFail("参数错误");
        }

        SysWorker bo = service.get(Integer.valueOf(lid));
        sysFileService.getFiles(bo, "idcard");
        sysFileService.getFiles(bo, "certificate");
        return sendSuccess(bo);
    }

    @ResponseBody
    @RequestMapping(params = "method=list")
    public ResponseVo list(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        QueryFilter filter = new QueryFilter(request, true);
        List<SysWorker> list = service.getAll(filter);

        return sendSuccess(list, filter.getPageBean().getTotals());
    }

    @RequestMapping(params = "method=toAdd")
    public ModelAndView toAdd(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(pageUrl)){
            return null;
        }
        SysFileModule idcard = sysFileModuleService.getByTableNameAndModuleEname("sys_worker", "idcard");
        SysFileModule certificate = sysFileModuleService.getByTableNameAndModuleEname("sys_worker", "certificate");

        model.addObject("sysFileModule_idcard", idcard.getLid());
        model.addObject("sysFileModule_certificate", certificate.getLid());
        model.addObject("uuid", UUID.randomUUID().toString());
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=add")
    public ResponseVo add(HttpServletRequest request, SysWorker bo){
        setOperatorToBo(bo);
        service.add(bo);
        return sendSuccess();
    }

    @ResponseBody
    @RequestMapping(params = "method=delete")
    public ResponseVo delete(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if (StringUtils.isBlank(lid)) {
            return sendFail("参数错误");
        }
        service.removeById(Integer.valueOf(lid));
        return sendSuccess();
    }

    @RequestMapping(params = "method=toEditOrDetail")
    public ModelAndView toEditOrDetail(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
        String lid = request.getParameter("lid");
        String pageUrl = request.getParameter("pageUrl");
        if(StringUtils.isAnyBlank(lid, pageUrl)){
            return null;
        }

        SysWorker bo = service.get(Integer.valueOf(lid));

        SysFileModule idcard = sysFileModuleService.getByTableNameAndModuleEname("sys_worker", "idcard");
        SysFileModule certificate = sysFileModuleService.getByTableNameAndModuleEname("sys_worker", "certificate");
        model.addObject("sysFileModule_idcard", idcard.getLid());
        model.addObject("sysFileModule_certificate", certificate.getLid());
        model.addObject("bo", bo);
        model.setViewName(pageUrl);
        return model;
    }

    @ResponseBody
    @RequestMapping(params = "method=update")
    public ResponseVo update(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        String lid = request.getParameter("lid");
        if(StringUtils.isBlank(lid)){
            return sendFail("参数错误");
        }

        try {
            SysWorker oldBo = service.get(Integer.valueOf(lid));
            _BeanUtil.copyPropertiesFromRequest(oldBo, request);
            service.update(oldBo);
            return sendSuccess();
        } catch (IllegalAccessException | InvocationTargetException e) {
            log.error("", e);
            return sendError(e.getMessage());
        }
    }

}
